package com.txsj.scan.base;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.os.Build;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.WindowManager;

import com.nativec.tools.ModuleManager;
import com.txsj.scan.AppManager;
import com.txsj.scan.MyApplication;
import com.txsj.scan.R;
import com.txsj.scan.http.UrlInterface;
import com.txsj.scan.utils.PreferenceUtil;
import com.txsj.scan.utils.ToastUtils;
import com.txsj.scan.widget.SlidingLayout;
import com.txsj.scan.widget.TSnackbarView;
import com.txsj.scan.widget.TitleBarView;
import com.txsj.scan.widget.dialog.WaitDialog;

import java.util.Locale;


/**
 * Created by wwb on 2017/1/3.
 */
public abstract class BaseActivity<P extends BasePresenter> extends AppCompatActivity implements UrlInterface {

    protected P presenter;
    protected WaitDialog wd;
    protected TitleBarView mTitle;

    //扫码枪设置
    protected boolean mSwitchFlag = false;

    //防止多次点击
    protected long lastClickTime = 0L;
    protected static final int FAST_CLICK_DELAY_TIME = 500;  // 快速点击间隔

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        AppManager.addActivity(this, getClass());
//        initState();
        // 禁止所有的Activity 横屏
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

        if (enableSliding()) {
            SlidingLayout rootView = new SlidingLayout(this);
            rootView.bindActivity(this);
        }

        switchLanguage(PreferenceUtil.getInstance().getString("language", "zh"));

        presenter = providePresenter();
    }

    private void initState() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {//5.0及以上
            View decorView = getWindow().getDecorView();
            int option = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                    | View.SYSTEM_UI_FLAG_LAYOUT_STABLE;
            decorView.setSystemUiVisibility(option);
            getWindow().setStatusBarColor(getResources().getColor(R.color.theme_color));
        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {//4.4到5.0
            WindowManager.LayoutParams localLayoutParams = getWindow().getAttributes();
            localLayoutParams.flags = (WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS | localLayoutParams.flags);
        }
    }


    //p层
    protected abstract P providePresenter();

    /**
     * 设置扫码枪语言为中文
     *
     * @param language
     */
    protected void switchLanguage(String language) {
        Resources resources = getResources();
        Configuration config = resources.getConfiguration();
        DisplayMetrics dm = resources.getDisplayMetrics();
        config.locale = Locale.SIMPLIFIED_CHINESE;
        resources.updateConfiguration(config, dm);
        PreferenceUtil.getInstance().commitString("language", language);
    }

    /**
     * 是否滑动关闭页面
     *
     * @return
     */
    protected boolean enableSliding() {
        return true;
    }

    @Override
    protected void onStart() {
        super.onStart();
    }


    protected boolean isRepeatClick(){
        if (System.currentTimeMillis() - lastClickTime < FAST_CLICK_DELAY_TIME) {
            return true;
        }
        lastClickTime = System.currentTimeMillis();
        return false;
    }

    /**
     * 无参数的跳转
     *
     * @param clazz 传递进来的要跳转的目的地
     */
    public void startActivity(Class<?> clazz) {
        Intent intent = new Intent(this, clazz);
        startActivity(intent);
//        overridePendingTransition(R.anim.slide_right_in, R.anim.slide_left_out);
    }

    public void showLoading() {
        if (null == wd) {
            wd = new WaitDialog(this);
        }
        if (!wd.isShowing()){
            wd.show();
        }
    }

    public void showLoading(String text,boolean showLoading) {
        if (null == wd) {
            wd = new WaitDialog(this);
            wd.setContent(text,showLoading);
        }
        if (!wd.isShowing()){
            wd.show();
        }
    }

    public void hideLoading() {
        if (wd != null && wd.isShowing()) {
            wd.dismiss();
        }
    }

    public void showErrorMsg(String msg) {
        new TSnackbarView(mTitle, msg);
    }

    /**
     * Toast
     *
     * @param msg
     */
    public void t(String msg) {
        ToastUtils.t(BaseActivity.this, msg);
    }

    protected void askForOut() {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);

        builder.setTitle(getString(R.string.alert_diag_title)).
                setMessage(getString(R.string.are_you_sure_to_exit)).
                setPositiveButton(getString(R.string.ok),
                        new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                //close the module
                                ModuleManager.newInstance().setUHFStatus(false);
                                MyApplication.getInstance().onTerminate();
                            }
                        }).setNegativeButton(getString(R.string.cancel),
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.cancel();
                    }
                }).setCancelable(false).show();
    }

    @Override
    protected void onDestroy() {
        if (presenter != null) {
            presenter.onDestroy();
        }
        super.onDestroy();
        AppManager.removeActivity(this);
    }
}
